﻿using NJabber.Stream.Elements;
using NUnit.Framework;

namespace NJabber.Client
{
    [TestFixture]
    public class RosterItemTest
    {
        #region Equals method tests

        [Test]
        public void Equals_SameNamesAndSameJids_ReturnsTrue()
        {
            VerifyEqualsMethod("name", "some@jid.com", "name", "some@jid.com", true);
        }

        [Test]
        public void Equals_DifferendNamesAndSameJids_ReturnsFalse()
        {
            VerifyEqualsMethod("name1", "some@jid.com", "name2", "some@jid.com", false);
        }

        [Test]
        public void Equals_SameNamesAndDifferendJids()
        {
            VerifyEqualsMethod("name", "jid1@domain.com", "name", "jid2@domain.com", false);
        }

        private void VerifyEqualsMethod(string itemName1, JID itemJid1, string itemName2, JID itemJid2, bool expectedResult)
        {
            RosterItem rosterItem1 = new RosterItem(itemName1, itemJid1);
            RosterItem rosterItem2 = new RosterItem(itemName2, itemJid2);

            bool areEquals = rosterItem1.Equals(rosterItem2);

            Assert.AreEqual(expectedResult, areEquals);
        }

        #endregion

        [Test]
        public void Clone_CreatesSameRosterItem()
        {
            RosterItem item = new RosterItem("name", "some@jid.com");

            RosterItem clonedItem = (RosterItem)item.Clone();

            Assert.That(item, Is.EqualTo(clonedItem));
        }

        [Test]
        public void RosterItemElementCastOperaator_SomeRosterItem_CorrectlyCastToRosterItemElement()
        {
            RosterItem rosterItem = new RosterItem("item-name", "item@jid.com");

            RosterItemElement rosterItemElement = rosterItem;

            Assert.That(rosterItemElement.ItemName, Is.EqualTo(rosterItem.Name));
            Assert.That(rosterItemElement.Jid, Is.EqualTo(rosterItem.Jid));
            CollectionAssert.AreEquivalent(rosterItemElement.Groups, rosterItem.Groups);
        }
    }
}
